#include "FriendTask.h"

FriendTask::FriendTask(char *body, int len, int socketfd) : BaseTask::BaseTask(body, len, socketfd)
{
}

FriendTask::~FriendTask()
{
}

void FriendTask::work(SharedMemoryIPC*ipc)
{
    FRI_RESP fri_resp;
    FRI_REQ fri_req = {0};
    HEAD head={0};
    char send_buffer[1024]={0};
    memcpy(&fri_req, m_body,m_len);
    cout << "user_id:" << fri_req.id << endl;
    fri_resp.num = 0;
    UserModel model;
    vector<FRI_T> fri_list=model.query_all_Friend(fri_req.id);
    for(auto &e:fri_list)
    {
        if(e.friend_id!=fri_req.id)
        {
            fri_resp.users[fri_resp.num].id=e.friend_id;
            strcpy(fri_resp.users[fri_resp.num].name, e.friend_name);
            strcpy(fri_resp.users[fri_resp.num].icon, e.friend_path);
            
           
            fri_resp.num++;
        }
    }


    cout <<"num=" << fri_resp.num << endl;
    cout<<"1头像路径:"<<fri_resp.users[0].icon<<endl;
    cout<<"2头像路径:"<<fri_resp.users[1].icon<<endl;
    bzero(&head, sizeof(HEAD));
    bzero(send_buffer, sizeof(send_buffer));

    head.service_type = FRIEND;
    head.data_len = sizeof(fri_resp);
    cout<<"好友响应数据长度:"<<head.data_len<<endl;

    memcpy(send_buffer,&m_socketfd,sizeof(int));
    memcpy(send_buffer+sizeof(int), &head, sizeof(HEAD));
    memcpy(send_buffer +sizeof(int)+sizeof(HEAD), &fri_resp, head.data_len);
    // cout << "sizeof(HEAD) + head.data_len " << head.data_len << endl;
    // int len = write(m_socketfd, send_buffer, sizeof(HEAD) + head.data_len);
    // cout << "fri write len=" << len << endl;
    // cout << "fri type=" << head.service_type << endl;
    ipc->write_shm(send_buffer);
}
